/*
 * ComplexSearchWindow.java
 *
 * Created on __DATE__, __TIME__
 */

package Windows.Search;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import Tools.IMG;
import Tools.ToCenter;
import Windows.PicPanel;
import Windows.ShowWindows.ShowWindow;
import XML.XMLNode;

public class ComplexSearchWindow extends javax.swing.JFrame implements ActionListener, DocumentListener, MouseListener {
	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				new ComplexSearchWindow(null).setVisible(true);
			}
		});
	}
	XMLNode xf = new XMLNode(null, "seach");
	SeachTabbedPane stp;

	String tishi = "请输入关键字";

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JLabel jLabel1;

	private javax.swing.JLabel jLabel2;

	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JButton jbStart;
	public final javax.swing.JLabel jlTitle = new javax.swing.JLabel();
	private PicPanel jpBorder;
	private javax.swing.JPanel jpTop;
	private javax.swing.JTextField jtfKey;
	private javax.swing.JTextField jtfMaxSize;
	private javax.swing.JTextField jtfMinSize;
	private javax.swing.JTextField jtfType;
	/** Creates new form ComplexSearchWindow */
	public ComplexSearchWindow(SeachTabbedPane stp) {
		this.stp = stp;

		setContentPane(new PicPanel(IMG.ComSearch));// fenzubeijing.jpg
		jpTop = new PicPanel(IMG.ComTop);
		jpBorder = new PicPanel(IMG.ComB1, IMG.ComB2);

		initComponents();

		this.setTitle("高级搜索");
		jtfKey.setText(tishi);
		jtfKey.setForeground(Color.GRAY);

		this.jbStart.addActionListener(this);
		this.jtfKey.getDocument().addDocumentListener(this);
		this.jtfMinSize.getDocument().addDocumentListener(this);
		this.jtfMaxSize.getDocument().addDocumentListener(this);
		this.jtfType.getDocument().addDocumentListener(this);
		ToCenter.ToCenter(this);

		this.jLabel1.addMouseListener(this);
		this.jLabel2.addMouseListener(this);
		this.jLabel3.addMouseListener(this);
		this.jLabel4.addMouseListener(this);
		this.jLabel5.addMouseListener(this);
		this.jtfKey.addMouseListener(this);
		this.jtfMinSize.addMouseListener(this);
		this.jtfMaxSize.addMouseListener(this);
		this.jtfType.addMouseListener(this);

		this.addWindowListener(ShowWindow.getOpacityWindowsListener());
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Object obj = e.getSource();
		if (obj.getClass() == JButton.class) {
			JButton jb = (JButton) obj;
			if (jb.equals(this.jbStart)) {
				String keys = xf.get("keys");
				if (keys == null || keys.equals(tishi) || keys.length() == 0) {
					return;
				}
				stp.addSeach(xf);// 开始搜索
				this.setVisible(false);
				this.jtfKey.setText(this.tishi);
			}
		}
	}

	// End of variables declaration//GEN-END:variables

	@Override
	public void changedUpdate(DocumentEvent e) {
		// TODO Auto-generated method stub
		UpdateXML();
	}

	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jtfKey = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jtfMinSize = new javax.swing.JTextField();
		jtfType = new javax.swing.JTextField();
		jLabel4 = new javax.swing.JLabel();
		jtfMaxSize = new javax.swing.JTextField();
		jLabel5 = new javax.swing.JLabel();
		jbStart = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jlTitle.setFont(new java.awt.Font("宋体", 1, 18));
		jlTitle.setForeground(new java.awt.Color(204, 255, 255));
		jlTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		jlTitle.setText("\u9ad8\u7ea7\u641c\u7d22");

		javax.swing.GroupLayout jpTopLayout = new javax.swing.GroupLayout(jpTop);
		jpTop.setLayout(jpTopLayout);
		jpTopLayout.setHorizontalGroup(jpTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jpTopLayout.createSequentialGroup().addContainerGap().addComponent(jlTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE).addContainerGap()));
		jpTopLayout.setVerticalGroup(jpTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jpTopLayout.createSequentialGroup().addContainerGap().addComponent(jlTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 51, Short.MAX_VALUE).addContainerGap()));

		jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
		jLabel1.setText("\u5173\u952e\u5b57");
		jLabel1.setToolTipText("");

		jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
		jLabel2.setText("\u6587\u4ef6\u5927\u5c0f");
		jLabel2.setToolTipText("");

		jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
		jLabel3.setText("\u6587\u4ef6\u7c7b\u578b");
		jLabel3.setToolTipText("");

		jtfMinSize.setText("0");

		jtfType.setText("*");

		jLabel4.setForeground(new java.awt.Color(102, 204, 255));
		jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
		jLabel4.setText("* \u8868\u793a\u4efb\u610f\u7c7b\u578b");
		jLabel4.setToolTipText("");

		jtfMaxSize.setText("60M");

		jLabel5.setText("\u81f3");

		javax.swing.GroupLayout jpBorderLayout = new javax.swing.GroupLayout(jpBorder);
		jpBorder.setLayout(jpBorderLayout);
		jpBorderLayout.setHorizontalGroup(jpBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jpBorderLayout
						.createSequentialGroup()
						.addGap(62, 62, 62)
						.addGroup(
								jpBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jpBorderLayout.createSequentialGroup().addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jtfKey, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE))
										.addGroup(jpBorderLayout.createSequentialGroup().addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jtfType, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
										.addGroup(jpBorderLayout.createSequentialGroup().addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jtfMinSize, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jtfMaxSize, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))).addGap(116, 116, 116)));
		jpBorderLayout.setVerticalGroup(jpBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jpBorderLayout.createSequentialGroup().addGap(22, 22, 22).addGroup(jpBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1).addComponent(jtfKey, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(jpBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel2).addComponent(jtfMaxSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jtfMinSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel5)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addGroup(jpBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel3).addGroup(jpBorderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jtfType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel4))).addContainerGap(30, Short.MAX_VALUE)));

		jbStart.setText("\u641c \u7d22");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jpTop, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap(348, Short.MAX_VALUE).addComponent(jbStart, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap()).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jpBorder, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(jpTop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18).addComponent(jpBorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(10, 10, 10).addComponent(jbStart, javax.swing.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE).addContainerGap()));

		pack();
	}// </editor-fold>
		// GEN-END:initComponents

	@Override
	public void insertUpdate(DocumentEvent arg0) {
		// TODO Auto-generated method stub
		UpdateXML();
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		jpBorder.entered = true;
		// jpBorder.setBorder(new LineBorder(Color.BLUE));
		Object obj = e.getSource();
		if (obj.getClass() == JTextField.class) {
			JTextField jtf = (JTextField) obj;
			jtf.grabFocus();
			// log.print(String.valueOf(jtf.getSelectionStart()));
			jtf.setSelectionStart(jtf.getSelectionStart() == jtf.getText().length() ? 0 : jtf.getSelectionStart());
			if (jtf.equals(jtfKey)) {
				if (jtfKey.getText().equals(tishi)) {
					jtfKey.setText("");
					jtfKey.setForeground(Color.MAGENTA);
				}

			}
		}

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		jpBorder.entered = false;
		// jpBorder.setBorder(null);
		// jpBorder.updateUI();

		Object obj = e.getSource();
		if (obj.equals(jtfKey)) {
			if (jtfKey.getText().length() == 0) {
				jtfKey.setText(tishi);
				jtfKey.setForeground(Color.GRAY);
			}

		}
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeUpdate(DocumentEvent arg0) {
		// TODO Auto-generated method stub
		UpdateXML();
	}

	public void UpdateXML() {
		xf.put("keys", jtfKey.getText());
		xf.put("minsize", String.valueOf(Tools.Units.toByteLength(jtfMinSize.getText())));// 任意文件大小
		xf.put("maxsize", String.valueOf(Tools.Units.toByteLength(jtfMaxSize.getText())));// 任意文件大小
		xf.put("type", jtfType.getText());// 任务文件类型
	}
}